#include "modbus.h"
#include "speed_prot.h"
#include "astar.h"
#include "usart_device.h"
#include "user_device.h"

ushort Read_RW_word(ushort addr)
{

    return RW_WORD_FIRST[addr];
}

ushort Read_RO_word(ushort addr)
{
    return RO_WORD_FIRST[addr];
}

void Write_word(ushort addr, ushort val)
{
    RW_WORD_FIRST[addr] = val;
    switch(addr)
    {
        case 0x02:
            rw_word.X_MAX_DISTANCE = from_16_little_endian_32(rw_word.X_MAX_DISTANCE);
            break;
        case 0x05:
            rw_word.Y_MAX_DISTANCE = from_16_little_endian_32(rw_word.Y_MAX_DISTANCE);
            break;
        case 0x08:
            rw_word.X_REF_POS = from_16_little_endian_32(rw_word.X_REF_POS);
            break;
        case 0x0c:
            rw_word.X_TARGET_1 = from_16_little_endian_32(rw_word.X_TARGET_1);
            break;
        case 0x0e:
            rw_word.Y_TARGET_1 = from_16_little_endian_32(rw_word.Y_TARGET_1);
            break;
        case 0x10:
            rw_word.Z_TARGET_1 = from_16_little_endian_32(rw_word.Z_TARGET_1);
            break;
        case 0x14:
            rw_word.X_TARGET_2 = from_16_little_endian_32(rw_word.X_TARGET_2);
            break;
        case 0x15:
            rw_word.Y_TARGET_2 = from_16_little_endian_32(rw_word.Y_TARGET_2);
            break;
        case 0x16:
            rw_word.Z_TARGET_2 = from_16_little_endian_32(rw_word.Z_TARGET_2);
            break;
    }
    if (addr == 10)
    {
        pending_action = 1;
        ps_server_busy = 1;
    }
    else if (addr >= 0x19 && addr <= 0x28)
        astar_map_changed = 1;
}

